package com.agileai.bpm.controller.define;

import com.agileai.bpm.common.ProcessDefineUtil;
import com.agileai.bpm.controller.ui.VariableBox;
import com.agileai.bpm.model.InterceptorModelDocument.InterceptorModel;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.RestInvokerType;
import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfWebServiceInvokerEditHandler extends WfProcessInfoHandler {
	private static String selectProcVar = "selectProcVar";
	private static String addProcVar = "addProcVar";
	private static String selectReqObject = "selectReqObject";
	private static String addReqObject = "addReqObject";
	private static String CUR_ACTION = "curAction";
	
	public ViewRenderer prepareDisplay(DataParam param){
		this.mergeParam(param);
		this.setAttributes(param);
		this.initAttriubtes(param);
		return new LocalRenderer(getPage());
	}
	
	public ViewRenderer doSaveAction(DataParam param){
		String rspText = null;
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		ProcessDefine processDefine = getProcessDefine();
		InterceptorModel interceptorModel = null;
		if (StringUtil.isNullOrEmpty(interceptorId)){
			interceptorModel = ProcessDefineUtil.createInterceptorModel(processDefine, positionType, activityId);
			String id = KeyGenerator.instance().genKey();
			interceptorModel.setId(id);
		}
		else{
			interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine, positionType, activityId, interceptorId);
		}
		this.buildInterceptor(interceptorModel, param);
		rspText = SUCCESS;
		return new AjaxRenderer(rspText);
	}

	private void buildInterceptor(InterceptorModel interceptorModel,DataParam param){
		RestInvokerType wsInvokerType = interceptorModel.getRestInvoker();
		if  (wsInvokerType == null){
			wsInvokerType = interceptorModel.addNewRestInvoker();
		}
		String interceptorName = param.get("interceptorName");
		String variableCode = param.get("variableCode");
		String isSynchronous = param.get("isSynchronous");
		
		interceptorModel.setSynchronous(Boolean.parseBoolean(isSynchronous));
		interceptorModel.setName(interceptorName);
		interceptorModel.setVariableCode(variableCode);
		
		String httpURL = param.get("httpURL");
		wsInvokerType.setHttpURL(httpURL);
		String requestJson = param.get("requestJson");
		wsInvokerType.setRequestJson(requestJson);
	}
	
	public ViewRenderer doBackAction(DataParam param){
		this.storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfInterceptorTypeSelectHandler.class));
	}

	private void initAttriubtes(DataParam param){
		FormSelect formSelect = FormSelectFactory.create("InterceptorType");
		formSelect.setHasBlankValue(false);
		String interceptorType = param.get("interceptorType");
		formSelect.setSelectedValue(interceptorType);
		this.setAttribute("interceptorTypeSelect", formSelect);
		
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		
		ProcessDefine processDefine = getProcessDefine();
		if (!StringUtil.isNullOrEmpty(interceptorId)){
			InterceptorModel interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine
					,positionType, activityId, interceptorId);
			RestInvokerType restInvokerType = interceptorModel.getRestInvoker();
			String httpURL = restInvokerType.getHttpURL();
			String requestJson = restInvokerType.getRequestJson();
			
			String interceptorName = interceptorModel.getName();
			String variableCode = interceptorModel.getVariableCode();
			String isSynchronous = String.valueOf(interceptorModel.getSynchronous());
			if (!this.hasAttribute("interceptorName")){
				this.setAttribute("interceptorName", interceptorName);				
			}
			if (!this.hasAttribute("variableCode")){
				this.setAttribute("variableCode", variableCode);				
			}
			if (!this.hasAttribute("isSynchronous")){
				this.setAttribute("isSynchronous", isSynchronous);				
			}
			if (!this.hasAttribute("httpURL")){
				this.setAttribute("httpURL", httpURL);				
			}
			if (!this.hasAttribute("requestJson")){
				this.setAttribute("requestJson", requestJson);				
			}			
		}

		if (this.getAttribute("isSynchronous")==null){
			this.setAttribute("isSynchronous","false");
		}
		String varCode = param.get("varCode");
		if (!StringUtil.isNullOrEmpty(varCode)){
			String curAction = param.get(CUR_ACTION);
			if (selectProcVar.equals(curAction) || addProcVar.equals(curAction)){
				this.setAttribute("variableCode", varCode);	
			}
			else if (selectReqObject.equals(curAction) || addReqObject.equals(curAction)){
				this.setAttribute("requestXMLObject", varCode);
			}
		}
	}

	public ViewRenderer doShowVariableBoxAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doShowVariableBoxAction();
	}
	public ViewRenderer doChangeVariableTypeAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doChangeVariableTypeAction();
	}
	public ViewRenderer doSelectVariableAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		String varTargetId = param.get("varTargetId");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		if ("variableCode".equals(varTargetId)){
			return variableSelectBox.doSelectVariableCode();
		}else{
			return variableSelectBox.doSelectVariableAction();			
		}
	}
}
